package cn.org.hentai.jtt1078.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;

import org.junit.jupiter.api.Test;

import cn.org.hentai.jtt1078.util.ByteHolder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import lombok.SneakyThrows;

/**
 *
 * @author luoyh(Roy) - Aug 30, 2024
 * @since 21
 */
public class H264Segment {

    public static void main(String[] args) throws Exception {
        byte[] data = VideoPushTest.class.getResourceAsStream("/tcpdump.bin").readAllBytes();
        ByteBuffer bb = ByteBuffer.allocate(1024);
        int j = 0;
        for (int i = 0; i < data.length;) {
            bb.put(data, i + bb.position(), 16);
            int dt = (bb.get(15) >>> 4 & 0xf);
            if (dt != 4) {
                bb.put(data, i + bb.position(), 8);
            }
            if (dt < 3) {
                bb.put(data, i + bb.position(), 4);
            }
            bb.put(data, i + bb.position(), 2);
            bb.put(data, i + bb.position(), bb.getShort(bb.position() - 2));
            i += bb.position();
            byte[] cmd = new byte[bb.position()];
            bb.flip();
            bb.get(cmd);
            System.out.println(ByteBufUtil.hexDump(cmd));
            bb.clear();
            if (j++ > 10)
                return;
        }
    }
    
    @Test
    void copy() {

        byte[] data = new byte[1];
        data[0] = 0x01;
        byte[] buffer = new byte[1];
        System.arraycopy(data, 0, buffer, 0, 1);
        System.out.println(buffer.length);
        System.arraycopy(data, 0, buffer, 1, 1);
        System.out.println(buffer.length);
    }

    @Test
    @SneakyThrows
    void nalus() {
        System.out.println("start ===> ");
        FileOutputStream h264out = new FileOutputStream("D:\\documents\\Tencent Files\\185088901\\FileRecv\\flvs\\jtt1078.h264");
        byte[] data = VideoPushTest.class.getResourceAsStream("/tcpdump.bin").readAllBytes();
        ByteBuffer bb = ByteBuffer.allocate(1024);
        ByteBuf buf = Unpooled.buffer();
        int j = 0;
        for (int i = 0; i < data.length;) {
            bb.put(data, i + bb.position(), 16);
            int dt = (bb.get(15) >>> 4 & 0xf);
            if (dt != 4) {
                bb.put(data, i + bb.position(), 8);
            }
            if (dt < 3) {
                bb.put(data, i + bb.position(), 4);
            }
            bb.put(data, i + bb.position(), 2);
            bb.put(data, i + bb.position(), bb.getShort(bb.position() - 2));
            i += bb.position();
            
            // h264
            if ((bb.get(5) & 0x7f) == 98) {
                byte[] cmd = new byte[bb.position() - 30];
                bb.flip();
                bb.get(30, cmd);
                System.out.println("cmd: " + ByteBufUtil.hexDump(cmd));
                h264out.write(cmd);
                buf.writeBytes(cmd);
            }
            bb.clear();
            //if (j ++ > 30) break;
        }
        readNalu(buf.array());
    }
    byte[] readNalu(byte[] data) {
        ByteBuffer buffer = ByteBuffer.allocate(data.length);
        buffer.put(data);
        buffer.flip();
        int j = 0;
        int loop = 0;
        for (int i = 0; i < data.length - 3; i++) {
            int a = buffer.get(i + 0) & 0xff;
            int b = buffer.get(i + 1) & 0xff;
            int c = buffer.get(i + 2) & 0xff;
            int d = buffer.get(i + 3) & 0xff;
            if (a == 0x00 && b == 0x00 && c == 0x00 && d == 0x01) {
                if (i == j)
                    continue;
                byte[] nalu = new byte[i - j];
                buffer.get(j, nalu, 0, nalu.length);
                System.out.println(ByteBufUtil.hexDump(nalu));
                j = i;
            }
        }
        return null;
    }

}
